package com.aepryus.loom;

import java.util.*;
import java.text.*;
import com.aepryus.util.*;

public class MySQLPersistence extends Persistence {
	public MySQLPersistence(String dbClass,String dbURL,String username,String password) throws PersistenceException {
		super(dbClass, dbURL, username, password);
	}

// Persistence =========================================================================================
    public String toSQL (String value) {
        if (value == null) return "null";
        StringBuffer sb = new StringBuffer();
        sb.append('\'');
        for (int i=0;i<value.length();i++) {
            if (value.charAt(i) == '\'' ||
                    value.charAt(i) == '\\' ||
                    value.charAt(i) == ';')
                sb.append('\\');
            sb.append(value.charAt(i));
        }
        sb.append('\'');
        return sb.toString();
    }

    public String toSQL (Date value) {
        return value==null ? "null" : "'"+dateFormat.format(value)+"'";
	}
	public Date toDate(Object value) {
		return (Date)value;
	}

	public String toSQL (DateTime value) {
        return value==null ? "null" : "'"+timeFormat.format(value)+"'";
	}
	public DateTime toDateTime(Object value) {
		return value==null ? null : new DateTime((Date)value);
	}

// Static =========================================================================================
	private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	private static DateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}

